package com.community.controller;

import com.alibaba.fastjson.JSONObject;
import com.community.bean.Role;
import com.community.bean.User;
import com.community.interceptor.Auth;
import com.community.service.MenuService;
import com.community.service.UserService;
import com.community.util.DateUtils;
import com.community.util.Result;
import com.community.util.Search;
import com.community.util.SessionUtil;
import com.feng.fengDUtil.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *@Author 丰建立
 *@Date 2019/1/25 16:43
 * 用户
 */
@Auth("用户")
@RestController
@RequestMapping(value = "/sys")
public class SysAction {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/getUser")
    public Result getUser(Integer id) {
        User user = userService.selectById(User.class,id);
        return Result.success(user);
    }


    @RequestMapping(value = "/listUser")
    public Result listUser(Page page, Search search) {
        userService.listUser(page,search);
        return Result.success(page);
    }

    @RequestMapping(value = "/listRole")
    public Result listRole(Page page, Search search) {
        menuService.listRole(page,search);
        return Result.success(page);
    }

    @RequestMapping(value = "/allMenus")
    public Result allMenus(Integer roleId) {
        return Result.success(menuService.allMenus(roleId));
    }

    @RequestMapping(value = "/allRoles")
    public Result allRoles() {
        return Result.success(menuService.allRoles());
    }

    @RequestMapping(value="/delUser")
    public Result delUser(String ids){
        try {
            userService.deleteByIds(User.class,ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/delRole")
    public Result delRole(String ids){
        try {
            userService.deleteByIds(Role.class,ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/checkUniqueName_mute")
    public JSONObject checkUniqueName(@RequestParam(value = "id",required = false) Integer id,
                                      @RequestParam(value = "account") String account)
    {
        JSONObject ret = new JSONObject();
        ret.put("isUnique",userService.isUnique(account,id));
        return ret;
    }

    @RequestMapping(value="/saveUser")
    public Result saveUser(@RequestBody User user){
        try {
            if(user.getId()==null){
                user.setCreator(SessionUtil.getCurUserId());
                user.setCreateTime(DateUtils.now());
            }else{
                if(user.getPassword()==null){
                    User existUser = userService.selectById(User.class,user.getId());
                    user.setPassword(existUser.getPassword());
                }
                user.setModifier(SessionUtil.getCurUserId());
                user.setModifyTime(DateUtils.now());
            }
            userService.saveOrUpdate(user);
            return Result.success(user);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRole")
    public Result saveRole(@RequestBody Role role){
        try {
            if(role.getId()==null){
                role.setCreator(SessionUtil.getCurUserId());
                role.setCreateTime(DateUtils.now());
            }
            menuService.save(role);
            return Result.success(role);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改用户密码
     * @return 修改密码的结果
     */
    @RequestMapping(value="/changePassword",method= RequestMethod.POST)
    public Result handleChangePassword(@RequestParam String oldPass,@RequestParam String newPass ){
        User user = userService.selectById(User.class,SessionUtil.getCurUserId());
        if(!user.getPassword().equals(oldPass)){
            return Result.error("原密码错误！");
        }
        user.setPassword(newPass);
        userService.update(user);
        return Result.success();
    }
}
